package test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PermuteUnique1 {
	public List<List<Integer>> ret;
    public List<Integer> path;
    public boolean[] check;
    public void dfs(int[] nums){
        if(path.size()==nums.length){
            ret.add(new ArrayList(path));
            return ;
        }
        for(int i=0;i<nums.length;i++){
        	//只关注满足条件的情况
            if((check[i]==false)&&((i==0)||(nums[i]!=nums[i-1])||(check[i-1]==true))){
                check[i]=true;
                path.add(nums[i]);
                dfs(nums);
                path.remove(path.size()-1);
                check[i]=false;
            }
        }
    }
    public List<List<Integer>> permuteUnique(int[] nums) {
        ret=new ArrayList<>();
        path=new ArrayList();
        check=new boolean[nums.length]; 
        //先排序
        Arrays.sort(nums);
        dfs(nums);
        return ret;
    }

}
